﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VTLive.Models
{
    public class VideoRepository
    {
        VTliveDataContext db = new VTliveDataContext();

        public IQueryable<Video> GetAllVideos()
        {
            return db.Videos;
        }

        public Video GetVideo(int VideoID)
        {
            return (from vid in db.Videos
                   where VideoID == vid.ID
                   select vid).SingleOrDefault();
            //return db.Videos.SingleOrDefault(s => s.ID == VideoID);
        }

        public void Add(Video MyVideo)
        {
            db.Videos.InsertOnSubmit(MyVideo);
        }

        public int InsertReturnID(Video MyVideo)
        {
            db.Videos.InsertOnSubmit(MyVideo);
            db.SubmitChanges();
            return MyVideo.ID;
        }

        public IQueryable<Video> GetAllVideosOfSchedule(int id)
        {
            return from epi in db.Episodes
                   join vid in db.Videos on epi.VideoID equals vid.ID
                   where epi.ScheduleID == id
                   select vid;
        }

        public void Delete(Video MyVideo)
        {
            db.Videos.DeleteOnSubmit(MyVideo);
        }

        public void Save()
        {
            db.SubmitChanges();
        }
    }
}
